
using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using MIMS.WRS.Entities;
using MIMS.WRS.Data;
    
namespace MIMS.WRS.Business
{   
	/// <summary>
	/// 
	/// </summary> 
    public class BulkOrderService
    {
        #region BulkOrder

        public static void CreateBulkOrder(BulkOrder bulkOrder)
        {            
            try
            {
				using (TransactionScope scope = new TransactionScope()) 
				{
					if (bulkOrder.Catalogue.Length > 0) 
					{
						CatalogDAO catDAO = new CatalogDAO();						
						Catalog cat = catDAO.GetCatalog(bulkOrder.Catalogue);
						if (cat == null) 
						{
							cat = new Catalog();
							cat.CatalogName = bulkOrder.Catalogue;						
							if (cat.CatalogName.ToLower().StartsWith("lgd"))
								cat.CatalogOrder = 1;
							else
								cat.CatalogOrder = 99999;
							catDAO.CreateCatalog(cat);
						}
						bulkOrder.CatalogID = cat.CatalogID;
					}

					BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
					bulkOrderDAO.CreateBulkOrder(bulkOrder);

					scope.Complete();
				}                
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessCreateBulkOrderException, ex);
            }
        }        

        public static void UpdateBulkOrder(BulkOrder bulkOrder)
        {            
            try
            {
				using (TransactionScope scope = new TransactionScope()) 
				{
					if (bulkOrder.Catalogue.Length > 0) 
					{
						CatalogDAO catDAO = new CatalogDAO();						
						Catalog cat = catDAO.GetCatalog(bulkOrder.Catalogue);
						if (cat == null) 
						{
							cat = new Catalog();
							cat.CatalogName = bulkOrder.Catalogue;						
							if (cat.CatalogName.ToLower().StartsWith("lgd"))
								cat.CatalogOrder = 1;
							else
								cat.CatalogOrder = 99999;
							catDAO.CreateCatalog(cat);
						}
						bulkOrder.CatalogID = cat.CatalogID;
					}

					BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
					bulkOrderDAO.UpdateBulkOrder(bulkOrder);
					
					scope.Complete();
				}				       
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessUpdateBulkOrderException, ex);
            }
        }        

        public static void DeleteBulkOrder(int bulkOrderID)
        {            
            try
            {
                BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
                bulkOrderDAO.DeleteBulkOrder(bulkOrderID);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessDeleteBulkOrderException, ex);
            }
        }
        
        public static BulkOrder GetBulkOrder(int bulkOrderID)
        {            
            try
            {
                BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
                return bulkOrderDAO.GetBulkOrder(bulkOrderID);                
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetBulkOrderException, ex);
            }
        }                

        public static BulkOrderCollection GetBulkOrders(BulkOrderColumns orderBy, OrderDirection orderDirection)
        {            
            try
            {
                BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
                return bulkOrderDAO.GetBulkOrders(orderBy, orderDirection);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
            }
        }        

        public static BulkOrderCollection GetBulkOrders(BulkOrderColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
                return bulkOrderDAO.GetBulkOrders(orderBy, orderDirection, page, pageSize, out totalRecords);
            }
            catch (BusinessException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
            }
        }        

        #endregion

//		public static IDataReader GetBulkOrders() 
//		{
//			return GetBulkOrders(BulkOrderColumns.OrderDate, OrderDirection.ASC, 0, 0);
//		}

		public static BulkOrder GetBulkOrderDetails(int bulkOrderID)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrderDetails(bulkOrderID);                
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrderException, ex);
			}
		}      

		public static BulkOrderCollection GetBulkOrdersIDSHDReport(int conPointID, string loginName, int idsSort, int quantity,  int ownerId, int userId,  int target, int territoryId)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				bool isUSUser = loginName.ToLower().StartsWith("usa");				
				return bulkOrderDAO.GetBulkOrdersIDSHDReport(conPointID, isUSUser, idsSort, quantity, ownerId, userId, target, territoryId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		} 

		public static BulkOrderCollection GetBulkOrdersIDSReport(int conPointID, string loginName, int idsSort, int quantity,  int ownerId, int userId, int territoryId)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				bool isUSUser = loginName.ToLower().StartsWith("usa");				
				return bulkOrderDAO.GetBulkOrdersIDSReport(conPointID, isUSUser, idsSort, quantity, ownerId, userId, territoryId);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		} 

		public static BulkOrderCollection GetBulkOrdersSalesReport(int conPointID, string supplier, int owner, int type, int target, int both, int qty, string loginName)
		{            
			try
			{
				bool isUSUser = loginName.ToLower().StartsWith("usa");
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();						
				return bulkOrderDAO.GetBulkOrdersSalesReport(conPointID, supplier, owner, type, target, both, qty, isUSUser);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   

		public static IDataReader GetBulkOrdersAvailabilityReport(int conPointID, string supplier, int owner, int type, int target, int both, int qty, string loginName)
		{            
			try
			{
				bool isUSUser = loginName.ToLower().StartsWith("usa");
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrdersAvailabilityReport(conPointID, supplier, owner, type, target, both, qty, isUSUser);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   

		public static IDataReader GetBulkOrdersProStatsReport(int conPointID, string supplier, int owner, int type, int target,int both, int qty, string loginName)
		{            
			try
			{
				bool isUSUser = loginName.ToLower().StartsWith("usa");
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrdersProStatsReport(conPointID, supplier, owner, type, target, both, qty, isUSUser);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}

		public static IDataReader GetBulkOrdersCompletionDateSupplierReport(int conPointID, string supplier, int owner, int type, int target,int both, int qty, string loginName)
		{            
			try
			{
				bool isUSUser = loginName.ToLower().StartsWith("usa");
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrdersCompletionDateSupplierReport(conPointID, supplier, owner, type, target, both, qty, isUSUser);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   

		public static IDataReader GetBulkOrdersCompletionDateOwnerReport(int conPointID, string supplier, int owner, int type, int target,int both, int qty, string loginName)
		{            
			try
			{
				bool isUSUser = loginName.ToLower().StartsWith("usa");
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrdersCompletionDateOwnerReport(conPointID, supplier, owner, type, target, both, qty, isUSUser);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   

		public static IDataReader GetBulkOrdersOwnerBulkOrderReport(int conPointID, string supplier, int owner, int type, int target,int both, int qty, string loginName)
		{            
			try
			{
				bool isUSUser = loginName.ToLower().StartsWith("usa");
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrdersOwnerBulkOrderReport(conPointID, supplier, owner, type, target, both, qty, isUSUser);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   

		public static IDataReader GetBulkOrders(BulkOrderColumns orderBy, OrderDirection orderDirection, int page, int pageSize)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrders(orderBy, orderDirection, page, pageSize);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   

		/// <summary>
		/// 
		/// </summary>
		/// <param name="supplierCode"></param>
		/// <param name="type">0: incompleted, 1: completed order</param>
		/// <returns></returns>
		public static IDataReader GetBulkOrders(string supplierCode, int type, int sortType) 
		{
			return GetBulkOrders(supplierCode, type, sortType, 0, 0);
		}

		public static IDataReader GetBulkOrders(string supplierCode, int type, int sortType, int page, int pageSize)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetBulkOrders(supplierCode, type, sortType, page, pageSize);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}

		public static int CheckUniqueBONo(int owner, string BONo)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.CheckUniqueBONo(owner, BONo);
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}

		public static IDataReader GetProductList()
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetProductList();
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetBulkOrdersException, ex);
			}
		}   


		public static ProductDimensions GetProductDimensions(int bulkOrderID)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				return bulkOrderDAO.GetProductDimensions(bulkOrderID);                
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessGetProductDimensionsException, ex);
			}
		}    


		public static void InsertOrUpdateProductDimensions(ProductDimensions productDimension)
		{            
			try
			{
				BulkOrderDAO bulkOrderDAO = new BulkOrderDAO();
				bulkOrderDAO.CreateOrUpdateProductDimensions(productDimension);    
			}
			catch (BusinessException)
			{
				throw;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new BusinessException(SR.BusinessUpdateProductDimensionException, ex);
			}
		}        

		
    }
}